<?php
/**
 * @title 5518. 给定行和列的和求可行矩阵 - 剪枝
 * @author start2004
 */

ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $rowSum
     * @param Integer[] $colSum
     * @return Integer[][]
     */
    function restoreMatrix($rowSum, $colSum) {
        /**
         * @since 2020-10-03 行数,列数
         */
        $rowlen = count($rowSum);
        $collen = count($colSum);

        /**
         * @since 2020-10-10 返回矩阵
         */
        $result = array_fill(0, $rowlen, array_fill(0, $collen, 0));

        /**
         * @since 2020-10-10 迭代矩阵
         */
        $row = $col = 0;
        while($row < $rowlen AND $col < $collen){
            if($rowSum[$row] == $colSum[$col]){
                $result[$row][$col] = $rowSum[$row];

                /**
                 * @since 2020-10-10 行列各+1
                 */
                $row++;
                $col++;
            } elseif($rowSum[$row] > $colSum[$col]){
                $result[$row][$col] = $colSum[$col];
                $rowSum[$row] -= $colSum[$col];

                /**
                 * @since 2020-10-10 行不变, 列+1
                 */
                $col++;
            } else {
                $result[$row][$col] = $rowSum[$row];
                $colSum[$col] -= $rowSum[$row];

                /**
                 * @since 2020-10-10 列不变, 行+1
                 */
                $row++;
            }
        }

        /**
         * @return
         */
        return $result;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.03_8.php
 */
$datas = [
    // [
    //     [3,8],
    //     [4,7],
    // ],
    // [
    //     [14,9],
    //     [6,9,8],
    // ],
    // [
    //     [4,12,10,1,0],
    //     [1,0,3,16,7],
    // ],
    // [
    //     [10,2,7,0,117,13,21],
    //     [20,35,29,8,17,15,29,17],
    // ],
    [
        [2081,9903,9442,3927,4063,282,7924,6300,7159,3111,6591,3127,5482,3481,7285,9069,3440,9709,3345,218,7090,400,8208,5291,5186,2194,9085,9030,8732,6477,9858,9033,3994,1494,8285,6748,8019,4641,6952,8348,9771,1391,4620,4537,7706,7110,4966,3695,6659,7561,8880,8342,7147,379,1672,8170,9859,7367,74,9490,5929,1399,8733,527,1510,2214,2877,8882,9605,758,2965,5697,2925,2530,5874,2190,8849,3559,2509,8993,8571,3226,751,9711,4112,8838,4437,7612,7360,189,4418,8544,2228,2818,2469,5483,3207,8158,7471,9109,3453,9093,8543,2484,5871,8247,9909,5983,4552,5882,2149,3984,3203,3883,9591,841,5264,2371,5442,1621,909,4768,3320,1800,785,8481,2864,1204,7102,2288,5153,1242,4963,7610,227,6372,342,635,323,1819,9182,6933,4229,8980,9445,9251,4826,4311,2319,9155,3775,9864,1922,6172,7642,7521,4394,3410,2398,2894,8768,8701,7984,981,3647,4927,2280,8385,3928,6655,1539,7517,2715,8157,7997],
        [8148,9713,7487,3094,9611,5276,2599,1654,3255,6367,13,2941,8676,1282,2141,622,407,4226,8601,4876,8602,5232,7997,7522,6201,4231,4193,1772,4775,3152,1826,4928,1533,708,9141,6979,946,4207,25493,6293,9685,3135,9251,8069,7891,9169,4581,7331,7710,7568,5161,7360,2440,5214,398,1364,4441,7122,7717,3281,9473,7390,1031,6348,9849,3095,6983,3199,8634,6061,8745,6411,1199,7402,2380,6251,1144,3624,7448,3259,7883,7299,91,3053,2706,225,3603,7438,3460,7397,4436,8142,336,7343,981,4731,7089,799,6603,3170,3841,8702,8775,299,5796,1909,9399,3263,3869,4762,6433,3172,293,6385,2548,297,431,2924,3440,5914,389,5016,6984,6655,2659,6230,9171,8344,4062,746,8346,7022,9101,6694,5438,6266,1911,6141,2669,6674,995,7050,7192,5249,8368,3918,3491,3131,7443,7057,9869,4422,7577,8938,9366,5871,3038,634,4480,7824,3792,2924,9848,5962,3953,9910,3354,9397,4562,37,6934,6198,276,7631,7144,461,106,1879,3444]
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof(false);
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->restoreMatrix(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->find(...$data);
    // var_dump($result);
    if(count($result)<=20){
        var_dump($result);
    } else {
        var_dump(count($result));
    }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
